<?php

interface TFsBOXViewInterface
	{
		static function renderErrors();
		static function renderNotices();
		static function renderShoutBoxTitleBar();
	}

abstract class TFsBOXAbstractView implements TFsBOXViewInterface
	{
		static function renderButton($title, $url, $image, $extra_attrs = NULL, $num_of_tabs = 0)
			{
				$tabs = str_repeat("\t", $num_of_tabs);
				$with_icon_begin = ($image) ? "<span class=\"with_icon\" style=\"background-image: url($image);\">" : "";
				$with_icon_end = ($image) ? "</span>" : "";
				echo $tabs . "<a class=\"button\" href=\"$url\"$extra_attrs>{$with_icon_begin}{$title}{$with_icon_end}</a>\n";
			}

		static function renderErrors($hide = false)
			{
				echo "<blockquote id=\"tfsbox_error_dialog\"";
				if ($hide)
					echo " style=\"display: none;\"";
				echo ">\n";
				echo "\t<div id=\"tfsbox_error_dialog_content\">The following errors occurred:\n";
				if (TFsBOX::$Session->hasErrors())
				{
					echo "\t<ul>\n";
					foreach (TFsBOX::$Session->errors() as $value)
						echo "\t\t<li>$value</li>\n";
					echo "\t</ul>\n";
				}
				echo "\t</div>\n";
				echo "\t<div id=\"tfsbox_error_dialog_btns\">\n";
				TFsBOXAbstractView::renderButton("Close", "javascript: hide_dialog('tfsbox_error_dialog');", TFsBOXSession::urlRoot()."/images/icons/window-close.png", NULL, 1);
				echo "\t</div>\n";
				echo "</blockquote>\n";
			}

		static function renderInputDialog()
			{
				$cp = (defined('TFsBOX_CONTROL_PANEL')) ? "/tfsbox/cpanel" : "";
				echo "<form action=\"". $_SERVER['SCRIPT_NAME'] ."$cp\" class=\"tfsbox_dialog\" name=\"input_dialog_form\" id=\"tfsbox_input_dialog\">\n";
				echo "\t<div class=\"tfsbox_dialog_header\" id=\"tfsbox_input_dialog_header\">TFsBOX</div>\n";
				echo "\t<div id=\"tfsbox_input_dialog_content\"></div>\n";
				echo "\t<div class=\"tfsbox_dialog_btns\">\n";
				TFsBOXAbstractView::renderButton("OK", "javascript: document.input_dialog_form.submit();", TFsBOXSession::urlRoot()."/images/icons/dialog-ok.png", NULL, 1);
				TFsBOXAbstractView::renderButton("Cancel", "javascript: hide_input_dialog();", TFsBOXSession::urlRoot() ."/images/icons/window-close.png", NULL, 1);
				echo "\t</div>\n";
				echo "</form>\n";
			}

		static function renderMessageDialog()
			{
				echo "<div class=\"tfsbox_dialog\" id=\"tfsbox_msg_dialog\">\n";
				echo "\t<div class=\"tfsbox_dialog_header\" id=\"tfsbox_msg_dialog_header\">TFsBOX</div>\n";
				echo "\t<div id=\"tfsbox_msg_dialog_text\"></div>\n";
				echo "\t<div class=\"tfsbox_dialog_btns\">\n";
				TFsBOXAbstractView::renderButton("Close", "javascript: hide_msg_dialog();", TFsBOXSession::urlRoot() ."/images/icons/window-close.png", NULL, 1);
				echo "\t</div>\n";
				echo "</div>\n";
			}

		static function renderNotices($hide = false)
			{
				echo "<blockquote id=\"tfsbox_notice_dialog\"";
				if ($hide)
					echo " style=\"display: none;\"";
				echo ">\n";
				echo "\t<div id=\"tfsbox_notice_dialog_content\">\n";
				if (TFsBOX::$Session->hasNotices())
				{
					echo "\t<ul>\n";
					foreach (TFsBOX::$Session->notices() as $value)
						echo "\t\t<li>$value</li>\n";
					echo "\t</ul>\n";
				}
				echo "\t</div>\n";
				echo "\t<div id=\"tfsbox_notice_dialog_btns\">\n";
				TFsBOXAbstractView::renderButton("Close", "javascript: hide_dialog('tfsbox_notice_dialog');", TFsBOXSession::urlRoot()."/images/icons/window-close.png", NULL, 1);
				echo "\t</div>\n";
				echo "</blockquote>\n";
			}

		static function renderShoutBoxTitleBar()
			{
				echo "<div id=\"tfsbox_header\">\n";
				echo TFsBOX::Name ." (v". TFsBOX::Version .")\n";
				echo "<div style=\"float: right;\">\n";

				if (defined('TFsBOX_INDEX'))
				{
					if (TFsBOX::$Config->value('auto_refresh', 'no') == 'no')
						echo "<a class=\"with_icon\" href=\"javascript: refresh_shouts(TFSBOX_SCRIPT_PATH, true);\" style=\"background-image: url(". TFsBOXSession::urlRoot() ."/images/icons/view-refresh.png);\">Refresh Shouts</a>\n";
					else
						echo "<input type=\"checkbox\" id=\"tfsbox_refresh_shouts_cb\" checked=\"checked\"> Auto-refresh Shouts\n";
				}

				// MySQL
				$login_prefix = "";
				if (TFsBOX::$Database->databaseMode())
					$login_prefix .= "Register / ";

				if (TFsBOX::$Session->loggedIn())
				{
					if (!defined("TFsBOX_CONTROL_PANEL"))
						echo "<a class=\"with_icon\" href=\"". $_SERVER['SCRIPT_NAME'] ."/tfsbox/cpanel\" style=\"background-image: url(". TFsBOXSession::urlRoot() ."/images/icons/configure.png);\">Control Panel</a>\n";
					else
						echo "<a class=\"with_icon\" href=\"". $_SERVER['SCRIPT_NAME'] ."\" style=\"background-image: url(". TFsBOXSession::urlRoot() ."/images/icons/user-home.png);\">Back to Shouts</a>\n";

					echo "<a class=\"with_icon\" href=\"". $_SERVER['SCRIPT_NAME'] ."/tfsbox/logout\" style=\"background-image: url(". TFsBOXSession::urlRoot() ."/images/icons/system-log-out.png);\">Log Out</a>\n";
				}

				else if (!defined("TFsBOX_CONTROL_PANEL")
					  && !defined("TFsBOX_USER_LOGIN"))
					echo "<a class=\"with_icon\" href=\"". $_SERVER['SCRIPT_NAME'] ."/tfsbox/login\" style=\"background-image: url(". TFsBOXSession::urlRoot() ."/images/icons/login.png);\">{$login_prefix}Sign In</a>\n";
				else
					echo "<a class=\"with_icon\" href=\"". $_SERVER['SCRIPT_NAME'] ."\" style=\"background-image: url(". TFsBOXSession::urlRoot() ."/images/icons/user-home.png);\">Back to Shouts</a>\n";

				echo "</div>\n";
				echo "</div>\n";
			}

		static function renderSmileyPackOptions($selected = NULL)
		{
			foreach (TFsBOX::$Database->smileyPacks() as $pack)
			{
				$pack = str_replace(dirname(__FILE__)."/", "", $pack);
				echo "<option";
				echo ($selected == $pack) ? " selected=\"selected\"" : "";
				echo " value=\"$pack\">".basename($pack)."</option>\n";
			}
		}

		static function renderTooltip()
			{
				echo "<div id=\"tfsbox_tooltip\"></div>\n";
			}
	}

abstract class TFsBOXAbstractShoutView extends TFsBOXAbstractView
	{
		abstract function renderShoutForm();
		abstract function renderShouts();
	}

?>
